package cn.jcet.common.vaild;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 鲜磊
 * @Date 2020/7/7 16:22
 */
public class ListConstraintValidator implements ConstraintValidator<ListVaild,Integer> {

    Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(ListVaild constraintAnnotation) {
        // 循环其中的数据
        int[] vals = constraintAnnotation.vals();
        if (vals != null){
            for (int val : vals) {
                set.add(val);
            }
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        //  set中是否包含注解中的内容
        return set.contains(value);
    }
}
